package bpm.service.impl.user.unite;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import bpm.dao.user.unite.UniteDeptUserDao;
import bpm.domain.user.unite.UniteDeptUser;
import bpm.domain.user.unite.UniteUser.OperationState;
import bpm.service.user.unite.UniteDeptUserService;
import bpm.service.user.unite.UniteUserService;
/**
 * 
 * @author ZhaoWangLong Oct 27, 2011
 *
 */
@Service("uniteDeptUserService")
@Transactional
public class UniteDeptUserServiceBean implements UniteDeptUserService {
	@Resource UniteDeptUserDao uniteDeptUserDao;
	@Resource UniteUserService uniteUserService;
	private final Logger logger=Logger.getLogger(UniteDeptUserServiceBean.class);
	public void addMiddle(UniteDeptUser uniteDeptUser) {
		String deptCode=uniteDeptUser.getDeptCode();
		String userId=uniteDeptUser.getUserID();
		String state=uniteDeptUser.getUserDeptState();
		String versioned=uniteDeptUser.getVersioned();
		UniteDeptUser old=find(deptCode, userId);
		OperationState optState =BuildOperationState.creatOpeState(state, versioned);
		StringBuffer str=new StringBuffer();
		str.append("deptCode = ").append(deptCode).append(" , userId = ").append(userId)
			.append(" , delete = ").append(state).append(" versioned = ").append(versioned).append(" ; ");
		logger.debug(str.toString());
		uniteDeptUser.setOptSate(BuildOperationState.buildState(old==null?null:old.getOptSate(), optState));
		if(old!=null){
			this.uniteDeptUserDao.updateMiddle(uniteDeptUser);
		}else{
			this.uniteDeptUserDao.addMiddle(uniteDeptUser);
		}
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UniteDeptUser find(String deptCode, String userId) {
		return this.uniteDeptUserDao.findUniteDeptUser(deptCode, userId);
	}

}
